#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月13日

@author: Administrator
'''
from common import settings
from django.utils.decorators import available_attrs
from funcresource.models import UserFunctional
from functools import wraps
import copy


def user_funcresource(view_func):
    """
                    用户功能资源
    """
    def wrapped_view(request, *args, **kwargs):
        rsp_data = copy.copy(settings.ERROR["SUCC"])
        rsp_data["data"] = []
        if request.user:
            user_funcs = UserFunctional.objects.filter(func__func_resource__url=request.META["PATH_INFO"], user=request.user, func__state=settings.StateEnum.VALID, state=settings.StateEnum.VALID).order_by("id")
            for user_func in user_funcs:
                record = {
                    "name":user_func.func.name,
                    "label_css":user_func.func.label_css,
                    "icon":user_func.func.icon.css_class if user_func.func.icon else "",
                }
                rsp_data["data"].append(record)
        request.func_resous = rsp_data["data"]
        return view_func(request, *args, **kwargs)
    return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
